package com.zhumiao.weixindev.service;

import com.alibaba.fastjson.JSONObject;
import com.zhumiao.weixindev.entity.AccessToken;
import com.zhumiao.weixindev.entity.menu.Button;
import com.zhumiao.weixindev.entity.menu.ClickButton;
import com.zhumiao.weixindev.entity.menu.Menu;
import com.zhumiao.weixindev.entity.menu.ViewButton;
import com.zhumiao.weixindev.util.WeixinUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

@Service
public class MenuService {

    private static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";

    public static void main(String[] args) {
        System.out.println("运行结果(如果是0代表成功)："+ createMenu(initMenu().toString()));
    }


    public static Integer createMenu(String menu) {
        Integer result = 0;
        String url = CREATE_MENU_URL.replace("ACCESS_TOKEN", WeixinUtil.getToken());
        RestTemplate template = new RestTemplate();
        JSONObject jsonObject = template.postForObject(url, menu, JSONObject.class);
        if (jsonObject != null) {
            result = jsonObject.getInteger("errcode");
        }
        return result;
    }
    /**
     * 组装菜单
     * @return
     */
    public static Menu initMenu(){
        Menu menu = new Menu();
        ClickButton button11 = new ClickButton();
        button11.setName("了解杰瑞教育");
        button11.setType("click");
        button11.setKey("11");
        ClickButton button12 = new ClickButton();
        button12.setName("加入杰瑞教育");
        button12.setType("click");
        button12.setKey("12");
        ViewButton button21 = new ViewButton();
        button21.setName("杰瑞教育官网");
        button21.setType("view");
        button21.setUrl("http://www.jerehedu.com");
        ViewButton button22 = new ViewButton();
        button22.setName("杰瑞教育新闻网");
        button22.setType("view");
        button22.setUrl("http://www.jredu100.com");
        ClickButton button31 = new ClickButton();
        button31.setName("杰小瑞");
        button31.setType("click");
        button31.setKey("31");
        Button button1 = new Button();
        button1.setName("杰瑞教育"); //将11/12两个button作为二级菜单封装第一个一级菜单
        button1.setSub_button(new Button[]{button11,button12});
        Button button2 = new Button();
        button2.setName("相关网址"); //将21/22两个button作为二级菜单封装第二个二级菜单
        button2.setSub_button(new Button[]{button11,button12});
        menu.setButton(new Button[]{button1,button2,button31});// 将31Button直接作为一级菜单
        return menu;
    }

}
